<script lang="ts" setup>
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { ref } from 'vue';

import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { Apis } from '#/api';

import BanModal from '../components/ban-modal/index.vue';
import CommentList from '../components/comment-list/index.vue';
import StickyModal from '../components/sticky-modal/index.vue';

const [BaseBanModal, baseBanModalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: BanModal,
});

const [BaseStickyModal, baseStickyModalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: StickyModal,
});

const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm() {
    message.info('onConfirm');
    // drawerApi.close();
  },
  onOpenChange(isOpen) {
    if (isOpen) {
    }
  },
});

const gridRef = ref<any>(null);
const currPost = ref<any>({});

interface RowType {
  category: string;
  color: string;
  id: string;
  price: string;
  productName: string;
  releaseDate: string;
}

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: false,
  schema: [
    {
      component: 'Input',
      fieldName: 'userId',
      label: '用户ID',
    },
    {
      component: 'Input',
      fieldName: 'content',
      label: '发布内容',
    },
  ],
  showCollapseButton: true,
  submitOnEnter: false,
  wrapperClass: 'grid-cols-1 md:grid-cols-4',
};

const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  scrollX: {
    enabled: true,
  },
  scrollY: {
    enabled: true,
  },
  columns: [
    { type: 'checkbox', width: 60 },
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'userId',
      title: '用户ID',
      minWidth: 120,
    },
    { field: 'content', title: '评论内容', minWidth: 200 },

    { field: 'status', title: '评论状态', width: 80 },
    // 是否已删除
    {
      field: 'isDeleted',
      title: '是否已删除',
      width: 100,
      slots: { default: 'isDeleted' },
    },
    { field: 'createdAt', title: '发布时间', width: 150 },
    { field: 'deletedAt', title: '删除时间', width: 150 },
  ],
  height: 'auto',
  keepSource: true,
  pagerConfig: {
    enabled: false,
  },
  rowConfig: {
    isCurrent: true,
  },
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await Apis.forum.reply.getCursorPage({
          params: {
            page: page.currentPage,
            pageSize: page.pageSize,
            limit: 200,
            ...formValues,
          },
        });
      },
    },
  },
};

const selectedRows = ref<any[]>([]);

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
  gridEvents: {
    scrollBoundary({ direction }) {
      switch (direction) {
        case 'bottom': {
          console.log('触发底部阈值');
          // loadList(20);
          break;
        }
        case 'left': {
          console.log('触发左侧阈值');
          break;
        }
        case 'right': {
          console.log('触发右侧阈值');
          break;
        }
        case 'top': {
          console.log('触发顶部阈值');
          break;
        }
      }
    },
    cellClick({ row }) {
      gridApi.grid.toggleCheckboxRow(row);
      selectedRows.value = gridApi.grid.getCheckboxRecords();
    },
    checkboxChange() {
      selectedRows.value = gridApi.grid.getCheckboxRecords();
    },
  },
});

function handleViewComment(row: any) {
  currPost.value = row;

  drawerApi.open();
}

function handleSticky(row: any) {
  currPost.value = row;
  baseStickyModalApi.open();
}

function handleBan(row: any) {
  console.log('handleBan', row);
  currPost.value = row;
  baseBanModalApi.setData({
    userId: row.userId,
    nickname: row.userId,
    businessId: row.id,
  });
  baseBanModalApi.open();
}
</script>

<template>
  <Page auto-content-height>
    <BaseBanModal module="reply" title="回复封禁" @success="gridApi.reload()" />
    <Drawer title="帖子评论">
      <CommentList :post-id="currPost.id" />
    </Drawer>

    <Grid ref="gridRef" table-title="评论回复列表">
      <template #toolbar-tools>
        <a-space>
          <!-- <a-button
            :disabled="selectedRows.length !== 1"
            type="primary"
            @click="handleViewComment(row)"
          >
            查看评论
          </a-button> -->
          <!-- <a-button
            :disabled="selectedRows.length !== 1"
            type="primary"
            @click="handleSticky(row)"
          >
            置顶
          </a-button> -->
          <!-- <a-button
            :disabled="selectedRows.length === 0"
            type="primary"
            @click="handleBatchPush()"
          >
            批量加入推送
          </a-button> -->
          <a-button
            :disabled="selectedRows.length !== 1"
            danger
            type="primary"
            @click="handleBan(selectedRows[0])"
          >
            封禁
          </a-button>
        </a-space>
      </template>
      <template #user="{ row }">
        <div class="flex flex-col text-left">
          <a-avatar :src="row.user.avatar" />
          <div>昵称：{{ row.user.nickname }}</div>
          <div>用户ID：{{ row.user.userId }}</div>
        </div>
      </template>

      <template #isDeleted="{ row }">
        <a-tag :color="row.deletedAt ? 'red' : 'green'">
          {{ row.deletedAt ? '已删除' : '未删除' }}
        </a-tag>
      </template>
      <template #operation="{ row }">
        <a-button size="small" type="link" @click="handleViewComment(row)">
          查看评论
        </a-button>
        <a-divider type="vertical" />
        <a-button size="small" type="link" @click="handleSticky(row)">
          置顶
        </a-button>
        <a-divider type="vertical" />
        <a-button danger size="small" type="link" @click="handleBan(row)">
          封禁
        </a-button>
      </template>
    </Grid>
  </Page>
</template>
